package com.example.distributedlock.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * SingleLock
 *
 * @author lx
 * @date 2024-03-21 14:26
 */
@RestController
public class SingleLockController {
    HashMap<String, Integer> map = new HashMap<>();

    @GetMapping("/set")
    public String setMap() {
        map.put("amount", 1);
        return "ok";
    }

    @GetMapping("/get")
    public String getAmount() throws InterruptedException {
        synchronized (this) {
            Integer integer = map.get("amount");
            if (integer == 0) {
                return "卖完了";
            }
            Thread.sleep(2000);
            map.put("amount", integer-1);
            return String.valueOf((integer-1));
        }
    }
}
